package com.le.tester.javaThink.fifth.chapter14_StreamingProgramming;

import java.util.Arrays;
import java.util.function.Predicate;
import java.util.stream.Stream;

/**
 * 描述：操作文件过滤流
 */
public class OptionalFilter {
    static String[] elements = {"Foo", "", "Bar", "Baz", "Bingo"};

    static Stream<String> testStream() {
        return Arrays.stream(elements);
    }

    static void test(String desc, Predicate<String> pred) {
        System.out.println("===" + desc + "===");
        for (int i = 0; i < elements.length; i++) {
            //在for循环中，每次都会启动一次流
            System.out.println(testStream()
                    .skip(i)
                    .findFirst()
                    .filter(pred));
        }
    }

    public static void main(String[] args) {
        test("true", str -> true);
        test("false", str -> false);
        test("str!=\"\"", str -> str != "");
        test("str.length()==3", str -> str.length() == 3);
        test("startWith(B)", str -> str.startsWith("B"));
    }
}
